terraform {
  required_providers {
    huaweicloud = {
      source = "local-registry/huaweicloud/huaweicloud"
      version = "= 1.34.1"
    }
  }
}

provider "huaweicloud" {
  region     = "cn-north-4"
  access_key = ""
  secret_key = ""
}

#Images data source for image_id
data "huaweicloud_images_image" "default" {
  most_recent = true
  visibility  = "public"
  name        = "CentOS 7.9 64bit"
}

#Create VPC
resource "huaweicloud_vpc" "vpc" {
  cidr       = "172.16.0.0/16"
  name       = "${var.template_name}_vpc"
}

#Create Subnet
resource "huaweicloud_vpc_subnet" "subnet" {
  vpc_id      = huaweicloud_vpc.vpc.id
  name        = "${var.template_name}_subnet"
  cidr        = "172.16.10.0/24"
  gateway_ip  = "172.16.10.1"
  primary_dns = "100.125.1.250"
}

#Create Security Group 
resource "huaweicloud_networking_secgroup" "default" {
  name   = "${var.template_name}_secgroup"
}

#Security Group Rule Resource for Module
resource "huaweicloud_networking_secgroup_rule" "allow_ping" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "icmp"
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}

resource "huaweicloud_networking_secgroup_rule" "allow_all" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = 1
  port_range_max    = 65535
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.default.id
}

#Create ECS
resource "huaweicloud_compute_instance" "web" {
  depends_on = [huaweicloud_vpc_subnet.subnet]
  count = 2
  image_id        = data.huaweicloud_images_image.default.id
  flavor_id   = "c6.large.2"
  security_group_ids  = [huaweicloud_networking_secgroup.default.id]
  name = var.ecs_instance_name
  system_disk_type = "SSD"
  system_disk_size = 40
  charging_mode     = "postPaid"  
  availability_zone   = data.huaweicloud_availability_zones.myaz.names[count.index]
  network {
    uuid = huaweicloud_vpc_subnet.subnet.id
  } 
  user_data   = file("./welcome.sh")
}

#Create ELB
resource "huaweicloud_lb_loadbalancer" "internet" {
  depends_on = [huaweicloud_vpc_subnet.subnet]
  vip_subnet_id = huaweicloud_vpc_subnet.subnet.subnet_id
  name          = "${var.template_name}_elb"
}

#Create EIP
resource "huaweicloud_vpc_eip" "myeip" {
  publicip {
    type = "5_bgp"
  }
  bandwidth {
    name        = "mybandwidth"
    size        = 10
    share_type  = "PER"
    charge_mode = "traffic"
  }
}

resource "huaweicloud_networking_eip_associate" "eip_1" {
  depends_on = [huaweicloud_lb_loadbalancer.internet,huaweicloud_vpc_eip.myeip]
  public_ip = huaweicloud_vpc_eip.myeip.address
  port_id   = huaweicloud_lb_loadbalancer.internet.vip_port_id
}

resource "huaweicloud_lb_listener" "listener_1" {
  protocol        = "HTTP"
  protocol_port   = 80
  loadbalancer_id = huaweicloud_lb_loadbalancer.internet.id
}

resource "huaweicloud_lb_pool" "group_1" {
  name        = "group_1"
  protocol    = "HTTP"
  lb_method   = "ROUND_ROBIN"
  listener_id = huaweicloud_lb_listener.listener_1.id
}

resource "huaweicloud_lb_monitor" "monitor_http" {
  pool_id        = huaweicloud_lb_pool.group_1.id
  type           = "HTTP"
  delay          = 5
  timeout        = 3
  max_retries    = 3
  url_path       = "/"
}

resource "huaweicloud_lb_member" "member" {
  depends_on = [huaweicloud_compute_instance.web]
  count = 2
  address       = huaweicloud_compute_instance.web[count.index].access_ip_v4
  protocol_port = 8000
  weight        = 1
  pool_id       = huaweicloud_lb_pool.group_1.id
  subnet_id     = huaweicloud_vpc_subnet.subnet.subnet_id
}

#RDS Resource
data "huaweicloud_availability_zones" "myaz" {}

#Create RDS
resource "huaweicloud_rds_instance" "default" {
  depends_on = [huaweicloud_vpc.vpc]
  count = 1
  name = "${var.template_name}_rds"
  flavor = "rds.mysql.n1.xlarge.2.ha"
  ha_replication_mode = "async"
  vpc_id      = huaweicloud_vpc.vpc.id
  subnet_id           = huaweicloud_vpc_subnet.subnet.id
  security_group_id   = huaweicloud_networking_secgroup.default.id
  availability_zone   = [
    data.huaweicloud_availability_zones.myaz.names[0],
    data.huaweicloud_availability_zones.myaz.names[1]
  ]
  db {
    type     = "MySQL"
    version  = "5.6"
    password = var.rds_admin_pass
  }
  volume {
    type = "CLOUDSSD"
    size = 100
  }
  backup_strategy {
    start_time = "00:00-01:00"
    keep_days  = 1
  }
}
